package com.ruoyi.factory.staffSafe.controller;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.factory.staffSafe.service.IStaffSafeService;
import com.ruoyi.factory.staffSafe.service.IYunXuetangService;
import com.ruoyi.factory.util.WordUtil;
import com.ruoyi.system.service.SysOutputLogService;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 * @ClassName StaffSafeController
 * @Description: TODO 安全环保部人员培训档案业务
 * @Author LiuJiaji
 * @Date 2024/4/9 16:46
 **/
@EnableScheduling
@RestController("staffSafe")
@RequestMapping("/api/staffSafeinfo")
public class StaffSafeController {

    @Autowired
    public IStaffSafeService iStaffSafeService;
    @Autowired
    public IYunXuetangService yunXuetangService;

    //手动同步数据
    @PostMapping("/updateStaffInfo")
    public void updateStaffInfo(@RequestBody String req, HttpServletRequest request){
        JSONObject jsonObject = JSONObject.fromObject(req);
        String sinsl1 = jsonObject.optString("sinsl");
        Integer integer = Integer.valueOf(sinsl1);
        String resl = jsonObject.optString("resl");
        Integer integer2 = Integer.valueOf(resl);
        //刷新人员类型 没有证件的都是普通 有证件先设置总负责人 接着设置不为管理人员的为特总人员
        iStaffSafeService.updateStaffInfo();
        //同步云学堂学员信息
        yunXuetangService.getyxtstaffinfo(integer);
        //插入培训记录
        yunXuetangService.getstudyrecord(integer2);
    }

    //自动同步数据每天凌晨2点执行
    @Scheduled(cron="0 0 2 * * ?")
    public void updateStaffInfo(){
        //刷新人员类型 没有证件的都是普通 有证件先设置总负责人 接着设置不为管理人员的为特总人员
        iStaffSafeService.updateStaffInfo();
        //同步云学堂学员信息
        yunXuetangService.getyxtstaffinfo(2);
        //插入培训记录
        yunXuetangService.getstudyrecord(3);
    }



    /**
     * 根据模板导出word
     */
//    @Log(title = "根据模板导出word", businessType = BusinessType.IMPORT)
//    @PreAuthorize("@ss.hasPermi('occupation:basics:exportWord')")
    @PostMapping("/exportWord")
    public AjaxResult exportWord(){
        Map<String, Object> map = new HashMap<>();

        map.put("ucode", "0001");
        map.put("username", "张三");
        map.put("sex", "男");
        map.put("usecardid", "110101199901012256");
        map.put("zw", "安全管理");
        map.put("byyx", "清华大学");
        map.put("sxzy", "安全管理");
        map.put("lxdh", "18826666666");
        map.put("aqzgzsid", "1234567890123456789");
        map.put("zsyxqzrq", "2024-04-19~2025-04-19");
        map.put("id", "13");

        String str = UUID.randomUUID().toString()+".docx";
        //获取yml配置地址
        String tempDir = "D:\\e\\Download";

        List<Map<String, Object>> alarms = new ArrayList<>();

        for (int i = 0; i < 4; i++) {
            Map<String, Object> alarm = new HashMap<>();
            alarm.put("pxsj","2024-04-19");
            alarm.put("pxnr", "安全培训"+i);
            alarm.put("pxfs", "线上培训"+i);
            alarm.put("kss", "3"+i);
            alarm.put("cj", "100"+i);
            alarm.put("skjs", "李四"+i);
            alarms.add(alarm);
        }

        map.put("alarms",alarms);
        List<Map<String, Object>> listm = new ArrayList<>();
        listm.add(map);
        listm.add(map);

        String name = WordUtil.easyPoiExport("C:\\Users\\Administrator\\Desktop\\pxdamb_gl.docx", tempDir, str, listm);
        return AjaxResult.success(name);
    }

}
